package com.qqtech.qquan.pub.controller;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.circle.model.CircleBaseVo;
import com.qqtech.qquan.circle.service.CircleBaseService;
import com.qqtech.qquan.pub.enums.PubConst;
import com.qqtech.qquan.pub.model.PubPromotion;
import com.qqtech.qquan.pub.model.PubPromotionQuery;
import com.qqtech.qquan.pub.model.PubPromotionVo;
import com.qqtech.qquan.pub.service.PubPromotionService;
import com.qqtech.qquan.pub.service.PubSysParamService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/pub/promotion")
public class PubPromotionController extends BaseWebController<PubPromotion, PubPromotionQuery> {
	@Resource
	private PubPromotionService pubPromotionService;
	@Resource
	private PubSysParamService pubSysParamService;
	@Resource
	private CircleBaseService circleBaseService;

	@Override
	protected BaseService<PubPromotion> getBaseService() {
		return pubPromotionService;
	}

	/**
	 * 列表
	 * 
	 * @param pubPromotionQuery
	 * @return
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public Result list(@RequestBody PubPromotionQuery pubPromotionQuery) {
		Result result = new Result();
		List<PubPromotionVo> list = pubPromotionService.queryList(pubPromotionQuery);
		result.put("list", list);
		return result;
	}

	/**
	 * 增加推广
	 * 
	 * @param communityBase
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/create")
	@ResponseBody
	public Result create(@RequestBody PubPromotionVo pubPromotionVo) {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		Integer circleId = null;
		PubPromotionVo promotionVo = null;
		CircleBaseVo circleBaseVo=null;
		String days = pubSysParamService.getByKey(PubConst.PARAM_PUB_PROMOTION_AUTO_EXPIRE_DAYS).getValue();

		if (StringUtil.isNotBlank(pubPromotionVo.getCircleIds())) {
			Iterator<String> it = Splitter.on(";").trimResults().omitEmptyStrings().split(pubPromotionVo.getCircleIds())
					.iterator();
			List<String> listIds = Lists.newArrayList(it);
			if (listIds.size() > 0) {
				for (String string : listIds) {
					circleId = Integer.valueOf(string);
					promotionVo = pubPromotionService.getPubPromotionVo(circleId, pubPromotionVo.getTargetBizzType(),
							pubPromotionVo.getSourceBizzId(), pubPromotionVo.getSourceBizzType());
					if (null == promotionVo) {
						pubPromotionVo.setTargetBizzId(circleId);
						pubPromotionVo.setAutoExpireAt(TimeUtil.addDays(TimeUtil.now(), Integer.valueOf(days)));
						pubPromotionVo.setCreatorId(curUserId);
						pubPromotionService.insert(pubPromotionVo);
					}else{
						circleBaseVo=circleBaseService.getById(circleId);
						result.addErr(circleBaseVo.getName()+"圈子已推广，请不要重复推广。");
						return result;
					}
				}
			}
		}
		return result;
	}
}
